<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <!--列表-->
      <div class="mui-card" v-for="(item,index) in goodsList" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="item.checked==1?true:false"></mt-switch>
            <img :src="item.picUrl">
            <div class="info">
              <h1>{{item.goodsName}}</h1>
              <p>
                <span class="price">￥{{item.price}}</span>
                <numbox :count="item.number" :id="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id, index)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!--结算-->
    <div class="mui-card" style="">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计(不含运费)</p>
            <p>
              已勾选商品<span class="red">{{goodsList.length}}</span>
              件， 总价 <span class="red">￥{{priceNum}}</span>
            </p>
          </div>
          <mt-button type="danger" @click="addOrder">去结算</mt-button>
        </div>
      </div>
      <!-- <p>{{$store.getters.getGoodsSelected}}</p> -->
    </div>
      <div class="sku">
        <!-- <van-popup v-model="isSku"> -->
          <div class="clearCache">
             <canvas id="q"></canvas>
          </div>
          <!-- </van-popup> -->
      </div>
  </div>
</template>

<script>
  import numbox from '../subcomponents/shopcar_numbox'
  import { getShopcarList } from '../../api/api'
   import NumberBox from '../subcomponents/goodsInfo_numberbox'
export default {
  components: {
    numbox
  },
  data() {
    return {
      userId:localStorage.getItem('userId'),
      goodsList:[],
      priceNum:0,
      isSku: false, // 支付宝支付弹出框
    }
  },

  created () {
    if(localStorage.getItem('username')==null){
      alert("亲，请先登录哦");
      this.$router.push('/login');
      return
    }
    this.getCar();
  },
  methods: {
    getCar(){
      var $this=this;
      this.axios({
      method:'get',
      url: "http://localhost:9999/market/ecCart/list",
      params:{userId:this.userId}
    }).then(function(res){
        console.log("查询的订单",res);
        $this.goodsList=res.data.obj;
        $this.goodsList.forEach(function(goods) {
          $this.priceNum+=goods.price;
        });
    });
    },
    remove(id,i) {
      this.goodsList.splice(i,1)
      this.$store.commit('removeCar',id)
    },
    addOrder(){
      console.log(this.goodsList);
      if(this.goodsList.length==0){
        // alert("亲,您的购物车是空的呢，快去购买商品吧！");
        this.$messagebox({
          title: '温馨提示',
          message: '您的购物车是空的',
          showCancelButton: true,
          confirmButtonText:"开始购物",
          cancelButtonText:"查看订单"
        }).then(action => {
          if(action == 'confirm'){
            console.log('购物');
            this.$router.push('/home/goodslist');
          }else{
            console.log('订单');
            this.$router.push('/Order');
          }
      })
        return;
      }
      //this.isSku = true;
      //this.$router.push("/search");
      var canvas = document.getElementById("q");
      var $this = this;
      this.axios({
        method: "post",
        url: "http://localhost:9999/system/alibaba/pay/createPayOrder",
        params: {
          access_token: localStorage.getItem("access_token"),
          orderNo: new Date().getTime(),
          userId:this.userId
        }
      }).then(function(res) {
          console.log("生成支付宝扫码二维码返回信息为{}",res);
          $this.QRCode.toCanvas(
          canvas,
          res.data.obj,
          function(error) {
            if (error) {
              console.error(error);
            }
            console.log("QRCode success!");
          }
        );
      });
    }
  }
}
</script>

<style scoped lang="scss">
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
      height: 60px;
    }
    h1 {
      font-size: 13px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .red {
    color:red;
  }
  .clearCache{
    height: 100%;
    align-self: auto;
    margin-left: 100px;
  }
}

</style>
